﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MvcShop.Models.Repositories
{
    public class OnlinePollRepository:BaseRepositoryBase<OnlinePoll>,IOnlinePollRepository<OnlinePoll>
    {
        public OnlinePollRepository()
        {
            this.EntitiesSetName = "OnlinePolls";
        }

        public OnlinePoll GetDefault()
        {
            var query = this.EntitySet.OrderByDescending(c => c.OnlinePollId).Select(c => c).FirstOrDefault();
            return query;
        }
    }

    public class PollOptionRepository : BaseRepositoryBase<PollOption>, IPollOptionRepository<PollOption>
    {
        public PollOptionRepository()
        {
            this.EntitiesSetName = "PollOptions";
        }

        public void Del(IEnumerable<int> values)
        {
            var query = this.EntitySet.Where(ContainsCriteria.ContainsExpression<PollOption, int>(c => c.PollOptionId, values)).ToList();
            foreach (var value in query)
            {
                this.dataContext.DeleteObject(value);
            }
            this.SaveChanges();
        }

        public void UpdataBallots(IEnumerable<int> values)
        {
            var query = this.EntitySet.Where(ContainsCriteria.ContainsExpression<PollOption, int>(c => c.PollOptionId, values)).ToList();
            foreach (var value in query)
            {
                value.Ballots++;
                this.Update(value.PollOptionId, value);
            }
            this.SaveChanges();
        }
    }
}
